package com.example.motioneventmodule

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.view.get

/**
 * 这个模块用来实现拖动图片的效果
 * lastX
 * lastY
 * eventX=event.getRawX()
 * eventY=event.getRawY()
 * down时,
 *  lastX=eventX;
 *  lastY=eventY;
 * move时，
 *  dx=eventX-lastX;
 *  dy=eventY-lastY;
 *  left=imageView.getLeft()+dx
 *  right=imageView.getRight()+dy
 *  top=imageView.getTop()+dy
 *  bottom=imageView.getBotton+dy
 *  imageView.layout(left,top,right,bottom)
 *
 *  为下次移动作准备：
 *      lastX=eventX
 *      lastY=eventY
 *
 */
class MainActivity : AppCompatActivity() {
//    private  val TAG = "MainActivity"
//    private lateinit var imageView: ImageView
//    private var lastX: Int = 0
//    private var lastY: Int = 0
//    private lateinit var linearLayout:LinearLayout
//    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//        setContentView(R.layout.activity_main)
//
//        imageView = findViewById(R.id.iv_main_pic)
//        linearLayout=imageView.parent as LinearLayout
//
//        val tempBottom = linearLayout.bottom
//        Log.e(TAG, "tempBottom:$tempBottom" )
//
//        val tempRight = linearLayout.right
//        Log.e(TAG, "tempRight:$tempRight")
//
//    }
//
//    //设置touch监听
//    override fun onTouchEvent(event: MotionEvent): Boolean {
//        val eventX = event.rawX.toInt()
//        val eventY = event.rawY.toInt()
//        when (event.action) {
//            MotionEvent.ACTION_DOWN -> {
//                this.lastX = event.rawX.toInt()
//                this.lastY = event.rawY.toInt()
//            }
//            MotionEvent.ACTION_MOVE -> {
//                val dx = eventX - lastX
//                val dy = eventY - lastY
//                var left = imageView.left + dx
//                var top = imageView.top + dy
//                var right = imageView.right + dx
//                var bottom = imageView.bottom + dy
//
//                //限制四个坐标
//                if (left < 0) {
//                    right += -left
//                    left = 0
//                }
//
//                if (top < 0) {
//                    bottom += -top
//                    top = 0
//                }
//                val tempRight = linearLayout.right
//
//                if (right > tempRight) {
//                    left -= (right-tempRight)
//                    right = tempRight
//                }
//
//                val tempBottom = linearLayout.bottom
//
//                if (bottom > tempBottom) {
//                    top -=(bottom-tempBottom)
//                    bottom = tempBottom
//                }
//                this.imageView.layout(left, top, right, bottom)
//                lastX = eventX
//                lastY = eventY
//            }
//        }
//        return true
//    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        val textView = MyTextView(this)
//        textView.setText("这是自定义TextView的文本")
//        setContentView(textView)
//        View.inflate()
        setContentView(R.layout.layout_2)
        //根据下标得到子View
        val linearLayout = View.inflate(this,R.layout.layout_2,null) as LinearLayout
        this.myTextView=linearLayout.getChildAt(0) as MyTextView
    }

    /**
     * 强制重新布局
     */
     lateinit var myTextView:MyTextView
    fun forceLayout(view: View) {

        myTextView.requestLayout()
        Toast.makeText(this,"强制重新而已",Toast.LENGTH_SHORT).show()
    }


}